package com.le.buddha.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;

import com.alibaba.fastjson.JSONObject;
import com.le.buddha.mobile.Good;
import com.le.buddha.mobile.Order;
import com.le.buddha.mobile.User;
import com.le.buddha.mobile.bo.WxOrderBO;
import com.le.buddha.service.*;
import com.wechat.pay.java.service.partnerpayments.app.model.Transaction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
public class PayServiceImpl implements IPayService {
    @Resource
    private IWxPayService wxPayService;
    @Resource
    private IGoodService goodsService;
    @Resource
    private IUserService userService;
    @Resource
    private IOrderService orderService;





    @Override
    public String wxCallback(HttpServletRequest request) throws Exception {

        Transaction transaction = wxPayService.callback(request);
        Order order = orderService.lambdaQuery().eq(Order::getOutTradeNo, transaction.getOutTradeNo()).one();
        if (order.getOrderStatus() == 0) {

            if (transaction.getTradeState().equals(Transaction.TradeStateEnum.SUCCESS)) {
                //支付成功
                Good good = goodsService.getById(order.getGoodId());
                order.setOrderTime(new Date());
                order.setOrderStatus(1);
                if (good.getGoodType() == 1) {
                    //购买逻辑1

                } else if (good.getGoodType() == 2) {
                    //购买逻辑2


                }

            } else if (transaction.getTradeState().equals(Transaction.TradeStateEnum.NOTPAY)) {
                //取消支付
                order.setOrderStatus(3);
            } else {
                //支付失败
                order.setOrderStatus(2);

            }

            orderService.updateById(order);
        }


        return "ok";
    }

    public static void main(String[] args) {
        Integer sharingAmount = (int) (290 * 0.2);


        double d = sharingAmount / 100.0;
        System.out.println(d);

    }
}
